﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Net;
using com.lover.common;
using System.IO;
using com.lover.astd.common.model;
using com.lover.common.http;

namespace com.lover.astd.common.partner.impl
{
    public class _29wwLogin : LoginImplBase
    {
        public _29wwLogin() : base() { }
        public override LoginResult login(ref List<Cookie> cookies, string verify_code = null, string extra = null)
        {
            LoginResult result = new LoginResult();
            logging();
            string loginurl = "http://29ww.com/CommonSource/UserLogin.ashx";
            string data = String.Format("uName={0}&uPwd={1}&CRM=true&method=Login", _username, _password);
            HttpResult resp = TransferMgr.doPostPure(loginurl, data, ref cookies);
            if (resp == null) { result.StatusCode = LoginStatusCode.FailInLogin; return result; }

            string retstr = resp.getContent();

            if (!retstr.Equals("Y"))
            {
                result.StatusCode = LoginStatusCode.FailInLogin;
                return result;
            }

            findingServerUrl();
            string serverlist_url = "http://29ww.com/Play/Index.html";
            resp = TransferMgr.doGetPure(serverlist_url, ref cookies);
            if (resp == null) { result.StatusCode = LoginStatusCode.FailInFindingGameUrl; return result; }
            string content = resp.getContent(Encoding.GetEncoding("GBK"));
            Regex re = new Regex("<ul id=\"GameServer128\" style=\"display:none\">((.|\n)*?)</ul>");
            Match m = re.Match(content);
            if (m == null || m.Groups == null || m.Groups.Count < 2) { result.StatusCode = LoginStatusCode.FailInFindingGameUrl; return result; }
            content = m.Groups[1].Value;

            m = Regex.Match(content,
                String.Format("<a target=\"_blank\" href=\"([^\"]*?)\">{0}服[^/]*</span></a>", 
                    translateNumber(_acc.ServerId))
                );
            if (m == null || m.Groups == null || m.Groups.Count < 2) { result.StatusCode = LoginStatusCode.FailInFindingGameUrl; return result; }

            string redirecturl = m.Groups[1].Value;
            goingToGameUrl();

            makeSureValidUri("http://29ww.com", ref redirecturl);
            resp = TransferMgr.doGetPure(redirecturl, ref cookies);
            if (resp == null) { result.StatusCode = LoginStatusCode.FailInGotoGameUrl; return result; }
            if (resp.StatusCode != 302) { result.StatusCode = LoginStatusCode.FailInGotoGameUrl; return result; }
            string location = resp.getExtraHeader("Location");
            if (location == null || location == "") { result.StatusCode = LoginStatusCode.FailInGotoGameUrl; return result; }
            makeSureValidUri("http://29ww.com", ref location);

            processRedirect(location, result, ref cookies);
            return result;
        }
        private string translateNumber(int number)
        {
            string[] numbers = new string[10] { "", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
            string result = number.ToString();
            if (number < 100)
            {
                if (number < 10) result = numbers[number];
                else
                {
                    int tens = number / 10;
                    int remain = number % 10;
                    result = tens > 1 ? numbers[tens] : "" + "十" + numbers[remain];
                }
            }
            return result;
        }
    }
}
